package com.jdb.banner.controller.assembler;

import com.alibaba.fastjson.JSON;
import com.jdb.banner.api.dto.BannerDTO;
import com.jdb.banner.provider.entity.BannerDO;
import org.springframework.cglib.beans.BeanCopier;

import java.util.Objects;

public class BannerAssembler {
    private static final BeanCopier fromCopier = BeanCopier.create(
            BannerDTO.class, BannerDO.class, false);
    private static final BeanCopier toCopier = BeanCopier.create(
            BannerDO.class, BannerDTO.class, false);

    public static BannerDO fromDTO(BannerDTO bannerDTO) {
        if (Objects.isNull(bannerDTO)) {
            return null;
        }
        BannerDO bannerDO = new BannerDO();
        fromCopier.copy(bannerDTO, bannerDO, null);
        bannerDO.setMeta(JSON.toJSONString(bannerDTO.getMeta()));
        return bannerDO;
    }

    public static BannerDTO toDTO(BannerDO bannerDO) {
        if (Objects.isNull(bannerDO)) {
            return null;
        }
        BannerDTO loanDTO = new BannerDTO();
        toCopier.copy(bannerDO, loanDTO, null);
        loanDTO.setMeta(JSON.parseObject(bannerDO.getMeta()));
        return loanDTO;
    }
}
